package com.socool.sinofaith.dao;

import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.socool.sinofaith.model.SifTaskBean;
import com.socool.sinofaith.util.AndroidSQLiteOpenHelper;
import com.socool.sinofaith.util.SinofaithUtil;

public class SifTaskDao {
	private AndroidSQLiteOpenHelper dbHelper;
	private String userMobile;
	public SifTaskDao(Context context) {
		dbHelper = new AndroidSQLiteOpenHelper(context);
		userMobile=getSPString(context);
	}

	// 插入记录
	public int insert(SifTaskBean sifTaskBean) {
		Log.i("SQLite", "----insert----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"insert into "
							+ SifTaskBean.TABLENAME
							+ " (Task_ID,TaskName,TaskDesc,IsPush,StartTM,"
							+ "EndTM,HasAddress,IsPublic,IsPreview,AnswerTimes,PhotoCompression,GoBackSet,Mobile,CreateTime,ModifyTime) values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
					new Object[] { sifTaskBean.getTask_ID(),
							sifTaskBean.getTaskName(),
							sifTaskBean.getTaskDesc(), sifTaskBean.getIsPush(),
							sifTaskBean.getStartTM(), sifTaskBean.getEndTM(),
							sifTaskBean.getHasAddress(),
							sifTaskBean.getIsPublic(),
							sifTaskBean.getIsPreview(),
							sifTaskBean.getAnswerTimes(),
							sifTaskBean.getPhotoCompression(),
							sifTaskBean.getGoBackSet(), 
							sifTaskBean.getMobile(),
							sifTaskBean.getCreateTime(),
							sifTaskBean.getModifyTime()});
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 删除记录
	public int delete(SifTaskBean sifTaskBean) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifTaskBean.TABLENAME
					+ " where Task_ID = ?",
					new Object[] { sifTaskBean.getTask_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 根据taskId删除记录
	public int deleteByTaskId(String taskId) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifTaskBean.TABLENAME
					+ " where Task_ID = ?", new Object[] { taskId });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 更新记录
	public int update(SifTaskBean sifTaskBean) {
		Log.i("SQLite", "----update----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"update "
							+ SifTaskBean.TABLENAME
							+ " set TaskName=?, TaskDesc=?, IsPush=?, StartTM=?, EndTM=?, HasAddress=?, IsPublic=?, IsPreview=?, AnswerTimes=?, PhotoCompression=?, GoBackSet=?, Mobile=?, CreateTime=?, ModifyTime=? where Task_ID=?",
					new Object[] { sifTaskBean.getTaskName(),
							sifTaskBean.getTaskDesc(), sifTaskBean.getIsPush(),
							sifTaskBean.getStartTM(), sifTaskBean.getEndTM(),
							sifTaskBean.getHasAddress(),
							sifTaskBean.getIsPublic(),
							sifTaskBean.getIsPreview(),
							sifTaskBean.getAnswerTimes(),
							sifTaskBean.getPhotoCompression(),
							sifTaskBean.getGoBackSet(),
							sifTaskBean.getMobile(),
							sifTaskBean.getCreateTime(),
							sifTaskBean.getModifyTime(),
							sifTaskBean.getTask_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 查询记录
	public ArrayList<SifTaskBean> query(String id) {
		Log.i("SQLite", "----query----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifTaskBean sifTaskBean;
		ArrayList<SifTaskBean> list = new ArrayList<SifTaskBean>();
		// 若fileId为null或""则查询所有记录
		if (id == null || id.equals("")) {
			cursor = db
					.rawQuery("select * from " + SifTaskBean.TABLENAME, null);
		} else {
			cursor = db.rawQuery("select * from " + SifTaskBean.TABLENAME
					+ " where Task_ID=?", new String[] { id });
		}
		while (cursor.moveToNext()) {
			sifTaskBean = new SifTaskBean();
			sifTaskBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifTaskBean.setTaskName(cursor.getString(cursor
					.getColumnIndex("TaskName")));
			sifTaskBean.setTaskDesc(cursor.getString(cursor
					.getColumnIndex("TaskDesc")));
			sifTaskBean.setIsPush(cursor.getString(cursor
					.getColumnIndex("IsPush")));
			sifTaskBean.setStartTM(cursor.getString(cursor
					.getColumnIndex("StartTM")));
			sifTaskBean.setEndTM(cursor.getString(cursor
					.getColumnIndex("EndTM")));
			sifTaskBean.setHasAddress(cursor.getString(cursor
					.getColumnIndex("HasAddress")));
			sifTaskBean.setIsPublic(cursor.getString(cursor
					.getColumnIndex("IsPublic")));
			sifTaskBean.setIsPreview(cursor.getString(cursor
					.getColumnIndex("IsPreview")));
			sifTaskBean.setAnswerTimes(cursor.getString(cursor
					.getColumnIndex("AnswerTimes")));
			sifTaskBean.setPhotoCompression(cursor.getString(cursor
					.getColumnIndex("PhotoCompression")));
			sifTaskBean.setGoBackSet(cursor.getString(cursor
					.getColumnIndex("GoBackSet")));
			sifTaskBean.setMobile(cursor.getString(cursor
					.getColumnIndex("Mobile")));
			sifTaskBean.setCreateTime(cursor.getString(cursor
					.getColumnIndex("CreateTime")));
			sifTaskBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifTaskBean.toString());
			list.add(sifTaskBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}

	// 自定义查询记录
	public ArrayList<SifTaskBean> queryBySql(String sql) {
		Log.i("SQLite", "----query----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifTaskBean sifTaskBean;
		ArrayList<SifTaskBean> list = new ArrayList<SifTaskBean>();
		cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			sifTaskBean = new SifTaskBean();
			sifTaskBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifTaskBean.setTaskName(cursor.getString(cursor
					.getColumnIndex("TaskName")));
			sifTaskBean.setTaskDesc(cursor.getString(cursor
					.getColumnIndex("TaskDesc")));
			sifTaskBean.setIsPush(cursor.getString(cursor
					.getColumnIndex("IsPush")));
			sifTaskBean.setStartTM(cursor.getString(cursor
					.getColumnIndex("StartTM")));
			sifTaskBean.setEndTM(cursor.getString(cursor
					.getColumnIndex("EndTM")));
			sifTaskBean.setHasAddress(cursor.getString(cursor
					.getColumnIndex("HasAddress")));
			sifTaskBean.setIsPublic(cursor.getString(cursor
					.getColumnIndex("IsPublic")));
			sifTaskBean.setIsPreview(cursor.getString(cursor
					.getColumnIndex("IsPreview")));
			sifTaskBean.setAnswerTimes(cursor.getString(cursor
					.getColumnIndex("AnswerTimes")));
			sifTaskBean.setPhotoCompression(cursor.getString(cursor
					.getColumnIndex("PhotoCompression")));
			sifTaskBean.setGoBackSet(cursor.getString(cursor
					.getColumnIndex("GoBackSet")));
			sifTaskBean.setMobile(cursor.getString(cursor
					.getColumnIndex("Mobile")));
			sifTaskBean.setCreateTime(cursor.getString(cursor
					.getColumnIndex("CreateTime")));
			sifTaskBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifTaskBean.toString());
			list.add(sifTaskBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}

	// 查询任务
	public ArrayList<SifTaskBean> querySifTaskBean(int currentIndex) {
		Log.i("SQLite", "----query----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifTaskBean sifTaskBean = null;
		String sql = "select * from " + SifTaskBean.TABLENAME
				+ " where Mobile=" + userMobile+" order by CreateTime desc limit "+(currentIndex-1)*SinofaithUtil.pageSize+","+SinofaithUtil.pageSize;
		Log.v("qqqq", sql);
		ArrayList<SifTaskBean> list = new ArrayList<SifTaskBean>();
		cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			sifTaskBean = new SifTaskBean();
			sifTaskBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifTaskBean.setTaskName(cursor.getString(cursor
					.getColumnIndex("TaskName")));
			sifTaskBean.setTaskDesc(cursor.getString(cursor
					.getColumnIndex("TaskDesc")));
			sifTaskBean.setIsPush(cursor.getString(cursor
					.getColumnIndex("IsPush")));
			sifTaskBean.setStartTM(cursor.getString(cursor
					.getColumnIndex("StartTM")));
			sifTaskBean.setEndTM(cursor.getString(cursor
					.getColumnIndex("EndTM")));
			sifTaskBean.setHasAddress(cursor.getString(cursor
					.getColumnIndex("HasAddress")));
			sifTaskBean.setIsPublic(cursor.getString(cursor
					.getColumnIndex("IsPublic")));
			sifTaskBean.setIsPreview(cursor.getString(cursor
					.getColumnIndex("IsPreview")));
			sifTaskBean.setAnswerTimes(cursor.getString(cursor
					.getColumnIndex("AnswerTimes")));
			sifTaskBean.setPhotoCompression(cursor.getString(cursor
					.getColumnIndex("PhotoCompression")));
			sifTaskBean.setGoBackSet(cursor.getString(cursor
					.getColumnIndex("GoBackSet")));
			sifTaskBean.setMobile(cursor.getString(cursor
					.getColumnIndex("Mobile")));
			sifTaskBean.setCreateTime(cursor.getString(cursor
					.getColumnIndex("CreateTime")));
			sifTaskBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifTaskBean.toString());
			list.add(sifTaskBean);
		}
		cursor.close();
		db.close();

		return list;
	}
	
	private String getSPString(Context context){
		SharedPreferences sp=context.getSharedPreferences("login", Context.MODE_PRIVATE);
		return sp.getString("UserMobile", "");
	}

}
